/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.grandcentrix.tray.util;

import net.grandcentrix.tray.provider.TrayContract;
import net.grandcentrix.tray.provider.TrayDBHelper;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import ohos.app.Context;

public class SqlcipherDbHelper extends SQLiteOpenHelper {
    public static final String TAG = SqlcipherDbHelper.class.getSimpleName();

    private static SqlcipherDbHelper instance;
    public boolean onConfigureCalled = false;

    public static final int DATABASE_VERSION_LEGACY = 1;
    public static final String DATABASE_NAME_LEGACY = "/data/user/0/net.grandcentrix.tray.sample/databases/tray.db";

    public static int DATABASE_VERSION = DATABASE_VERSION_LEGACY;
    public static String DATABASE_NAME = DATABASE_NAME_LEGACY;

    public static void setDbVerName(int version, String db_name) {
        if(DATABASE_VERSION_LEGACY == version || db_name.equalsIgnoreCase(DATABASE_NAME_LEGACY)) {
            LogUtil.warn(TAG, "You are using default version and db name, please change it as per your app.");
        }
        DATABASE_VERSION = version;
        DATABASE_NAME = db_name;
    }
    private static final String TEXT_TYPE = " TEXT";
    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + TrayDBHelper.TABLE_NAME + " (" +
                    TrayContract.Preferences.Columns.ID + " INTEGER PRIMARY KEY," +
                    TrayContract.Preferences.Columns.KEY + TEXT_TYPE + "," +
                    TrayContract.Preferences.Columns.VALUE + TEXT_TYPE + "," +
                    TrayContract.Preferences.Columns.MODULE + TEXT_TYPE + "," +
                    TrayContract.Preferences.Columns.CREATED + TEXT_TYPE + "," +
                    TrayContract.Preferences.Columns.UPDATED + TEXT_TYPE + "," +
                    TrayContract.Preferences.Columns.MIGRATED_KEY + TEXT_TYPE +
                    " )";

    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + TrayDBHelper.TABLE_NAME;

    public SqlcipherDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    static public synchronized SqlcipherDbHelper getInstance(Context context) {
        if (instance == null) {
            instance = new SqlcipherDbHelper(context);
        }
        return instance;
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onConfigure(SQLiteDatabase database){
        onConfigureCalled = true;
    }
}
